package com.heima.article.listen;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.article.service.ApArticleConfigService;
import com.heima.common.constants.message.NewsUpOrDownConstants;
import com.heima.model.article.pojos.ApArticleConfig;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 文章上架监听类
 * MQ
 */
@Component
public class ArticleUpOrDownListener {
    @Autowired
    private ApArticleConfigService apArticleConfigService;

    /**
     * 监听上架操作
     *
     * @param articleId
     */
    @RabbitListener(queuesToDeclare = {
            @Queue(value = NewsUpOrDownConstants.NEWS_UP_FOR_ARTICLE_CONFIG_QUEUE)
    })
    public void newsOn(String articleId) {
        apArticleConfigService.update(
                Wrappers.<ApArticleConfig>lambdaUpdate()
                        .ge(ApArticleConfig::getArticleId, Long.valueOf(articleId))
                        .set(ApArticleConfig::getIsDown, false));
    }

    /**
     * 监听下架操作
     *
     * @param articleId
     */
    @RabbitListener(queuesToDeclare = {
            @Queue(value = NewsUpOrDownConstants.NEWS_DOWN_FOR_ARTICLE_CONFIG_QUEUE)
    })
    public void newsOff(String articleId) {
        apArticleConfigService.update(
                Wrappers.<ApArticleConfig>lambdaUpdate()
                        .ge(ApArticleConfig::getArticleId, Long.valueOf(articleId))
                        .set(ApArticleConfig::getIsDown, true));
    }
}
